package while_lang.ast;

import java.util.*;

import while_lang.semantics.State;

/** Nodo del AST que representa una constante en el código: numérica, booleana
 *  o de cualquier otro tipo soportado.
 *  @author Leonardo Val
 */
public class Constant extends Exp {	
	public Object value; // Los valores se deben poner en wrappers.
	
	public Constant(Object value) {
		this.value = value;
	}

	@Override public String unparse() {
		return value.toString();
	}
	
	@Override public Object evaluate(State state) {
		return this.value;
	}
	
	public static Constant generate(Random random, int min, int max) {
		if (random.nextBoolean()) { // Generate as numeral.
			return new Constant((double)Math.min(random.nextInt(101), random.nextInt(101)));
		} else { // Generate as boolean value.
			return new Constant(random.nextBoolean());
		}
	}
	
	@Override public String toString() {
		return "Constant("+ value +")";
	}

	@Override public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	@Override public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Constant other = (Constant) obj;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}
	
}
